<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客列表</title>
    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/blog_list.css">
</head>

<body>
    <div class="nav">
        <img src="image/image1.jpg" alt="">
        <span class="title">个人博客系统</span>
        <span class="spacer"></span>
        <a href="login.html">登录</a>
        <a href="register.html">注册</a>
    </div>
    <div class="container">
        <div class="right">
            <div id="unart" style="text-align:center;display:none;">
                <h3>当前用户未发表文章</h3>
            </div>
        </div>
    </div>
    <script src="js/jquery.min.js"> </script>
    <script>
        jQuery.ajax({
            url: 'blog/getblogs',
            type: 'GET',
            success: function (result) {
                if (result != null && result.success == 200) {
                    var blogList = result.data;
                    if (blogList != null && blogList.length > 0) {
                        buildBlogs(blogList);
                    }
                } else {
                    alert("后端程序不可用!");
                }
            }
        })

        function buildBlogs(blogs) {

            let rightDiv = document.querySelector('.container .right');

            for (let blog of blogs) {
                //构造博客
                let blogDiv = document.createElement('div');
                blogDiv.className = 'blog';
                //构造博客的标题
                let titleDiv = document.createElement('div');
                titleDiv.className = 'title';
                titleDiv.innerHTML = blog.title;
                blogDiv.appendChild(titleDiv);
                //构造博客的时间
                let dateDiv = document.createElement('div');
                dateDiv.className = 'date';
                dateDiv.innerHTML = formatDate(blog.postTime);
                blogDiv.appendChild(dateDiv);
                //构造博客的正文
                let descDiv = document.createElement('div');
                descDiv.className = 'desc';
                descDiv.innerHTML = blog.content;
                blogDiv.appendChild(descDiv);
                //构造博客的查看全文
                let detailA = document.createElement('a');
                detailA.className = 'detail';
                detailA.innerHTML = '查看全文 &gt &gt;';
                detailA.href = 'blog_detail.html?blogId=' + blog.blogId;
                blogDiv.appendChild(detailA);

                rightDiv.appendChild(blogDiv);
            }
        }
        function formatDate(timeStampMS) {
            var date = new Date(timeStampMS);

            var year = date.getFullYear(),
                month = date.getMonth() + 1,
                day = date.getDate(),
                hour = date.getHours(),
                min = date.getMinutes(),
                sec = date.getSeconds();
            var newTime = year + '-' +
                (month < 10 ? '0' + month : month) + '-' +
                (day < 10 ? '0' + day : day) + ' ' +
                (hour < 10 ? '0' + hour : hour) + ':' +
                (min < 10 ? '0' + min : min) + ':' +
                (sec < 10 ? '0' + sec : sec);
            return newTime;
        }


    </script>
</body>

</html>